package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.BankListCityInfo;
import com.tiza.xiaoyoubao.entity.EditInfo;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by mapple.
 */
public class BankCityListInfoFragment extends BaseFragment {

    @BindView(R.id.lv_listView)
    ListView mListView;
    private CityAdapter mCityAdapter;
    private ArrayList<String> mCityInfos=new ArrayList<>();
    Bundle args;
    private String province;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_get_bank_city_info;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        args = getArguments();
        mCityInfos=args.getStringArrayList("city");
        province=args.getString("province");
        mCityAdapter = new CityAdapter();
        mListView.setAdapter(mCityAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String cityName = province+mCityInfos.get(position);
                EventBus.getDefault().post(new EditInfo(EditInfo.TYPE_GET_CITY_NAME, cityName),
                                           Constants.TAG_GETCITY_SUCCESS
                );
                finish();
            }
        });
    }

    class CityAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mCityInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return mCityInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.item_city_info, null);
                holder = new ViewHolder();
                holder.tvBankCity = (TextView) convertView.findViewById(R.id.tv_City);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvBankCity.setText(mCityInfos.get(position));
            return convertView;
        }
    }

    static class ViewHolder {

        public TextView tvBankCity;
    }

}
